package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.google.gson.Gson;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.User;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/12/6  14:30
 * 功 能：手环信息
 */
public class BraceletInformationActivity extends BaseActivity {

    @BindView(R.id.battery)
    TextView battery;
    @BindView(R.id.pedometer)
    TextView pedometer;
    @BindView(R.id.devicel)
    TextView devicel;
    @BindView(R.id.model)
    TextView model;
    @BindView(R.id.edition)
    TextView edition;
    @BindView(R.id.relative1)
    RelativeLayout relative1;
    @BindView(R.id.relative2)
    RelativeLayout relative2;
    @BindView(R.id.relative3)
    RelativeLayout relative3;
    @BindView(R.id.relative4)
    RelativeLayout relative4;
    @BindView(R.id.relative5)
    RelativeLayout relative5;
    @BindView(R.id.relative6)
    RelativeLayout relative6;
    private String patient_id,sos="",s_devicel;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bracelet_information;
    }

    @Override
    protected void initViews() {

        setToolbar(R.id.tool_bar);
        patient_id = this.getIntent().getStringExtra("p_id");
        getinfo();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONObject data = object.getJSONObject("data");

                        battery.setText(data.getString("battery"));
                        pedometer.setText(data.getString("pedometer"));
                        devicel.setText("设备号：" + data.getString("devicel"));
                        s_devicel = data.getString("devicel");

                        sos = data.getString("sos");

                        model.setText(data.getString("model"));
                        edition.setText(data.getString("edition"));

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("ss放大", "发到付" + e.toString());
                    }

                    break;

                case 2:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Toast(object.getString("msg"));

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("ss放大", "发到付" + e.toString());
                    }

                    break;
                case 3:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Toast(object.getString("msg"));

                        if (object.getInt("code") == 1){
                            finish();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    public void getinfo() {

        List<String> ls = new ArrayList<>();
        ls.add(patient_id);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getinfo");
        params.put("data[patient_id]", patient_id);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/watchApi", params, handler, 1, 404);

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.relative1, R.id.relative2, R.id.relative3, R.id.relative4, R.id.relative5, R.id.relative6})
    public void onViewClicked(View view) {

        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        Bundle bundle = new Bundle();
        switch (view.getId()) {

            case R.id.relative1:

                bundle.putString("patient_id",patient_id);
                startActivity(TheContactActivity.class,bundle);

                break;
            case R.id.relative2:
                alert_label();
                break;
            case R.id.relative3:

                bundle.putString("model",model.getText().toString());
                bundle.putString("devicel",s_devicel);
                bundle.putString("edition",edition.getText().toString());
                startActivity(AboutDeviceActivity.class,bundle);

                break;
            case R.id.relative4:
                Toast("网络正常！");
                break;
            case R.id.relative5:

                builder.setTitle("提示");
                builder.setMessage("是否关机？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast("请求关机中!");
                        setClose();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                });
                builder.show();

                break;
            case R.id.relative6:

                builder.setTitle("提示");
                builder.setMessage("是否解除设备？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        bindingDel();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                });
                builder.show();


                break;
        }
    }

    public void alert_label(){

        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

        LayoutInflater factory = LayoutInflater.from(this);//提示框
        final View view = factory.inflate(R.layout.dialog_sos, null);//这里必须是final的
        final EditText et = view.findViewById(R.id.editText);
        et.setText(sos);
        final EditText name = view.findViewById(R.id.name);
        et.setInputType(InputType.TYPE_CLASS_NUMBER);
        builder.setTitle("填写信息");

        final AlertDialog dialog = builder.create();
        dialog.setView(view);

        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();

            }
        });
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (!TextUtils.isEmpty(et.getText().toString()) && !TextUtils.isEmpty(name.getText().toString())) {

                    StringBuffer sb = new StringBuffer();
                    sb.append("[");
                    User user = new User();
                    user.setName(name.getText().toString());
                    user.setMobileNo(et.getText().toString());
                    Gson gson = new Gson();
                    String taskInfo = gson.toJson(user);
                    sb.append(taskInfo+"]");

                    setSos(sb.toString());
                    dialog.dismiss();

                } else {

                    Toast("非法输入!");

                }


            }
        });


    }

    public void setSos(String user) {

        List<String> ls = new ArrayList<>();
        ls.add(patient_id);
        ls.add(user);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "setSos");
        params.put("data[patient_id]", patient_id);
        params.put("data[sosList]", user);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/watchApi", params, handler, 2, 404);

    }

    public void setClose() {

        List<String> ls = new ArrayList<>();
        ls.add(patient_id);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "setClose");
        params.put("data[patient_id]", patient_id);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/watchApi", params, handler, 2, 404);

    }

    public void bindingDel() {

        List<String> ls = new ArrayList<>();
        ls.add(s_devicel);
        ls.add(patient_id);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "bindingDel");
        params.put("data[patient_id]", patient_id);
        params.put("data[devicel]", s_devicel);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/watch", params, handler, 3, 404);

    }


}
